<script setup>
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";
import {ref} from "vue";
import dayjs from "dayjs";
const levelType = ref({
  1: 'success',
  2: 'warning',
  3: 'danger'
})
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openDelete
} = useTable({
  requestFn: http.alarm,
  format: (res) => {
    return res.map(el => {
      return {
        ...el,
        levelType: levelType.value[el.level],
        device_no: el.device_no || '-',
        type: alarmCategoryList.value.find(item => item.value === el.type)?.name,
        level: alarmLevelList.value.find(item => item.value === el.level)?.name,
        created_at: dayjs(el.createdAt).format('YYYY-MM-DD HH:mm:ss'),
      }
    })
  },
  params: {
    type: '',
    level: '',
    device_no: '',
    start_time: dayjs().format('YYYY-MM-DD') + ' 00:00:00',
    end_time: dayjs().format('YYYY-MM-DD') + ' 23:59:59'
  }
})
const alarmCategoryList = ref([
  {
    name: '电力输出异常',
    value: '1'
  },
  {
    name: '光照强度异常',
    value: '2'
  },
  {
    name: '温度异常',
    value: '3'
  },
  {
    name: '电容量异常',
    value: '4'
  },
  {
    name: '电网指令和实际光储差值',
    value: '5'
  },
  {
    name: '日发电量异常',
    value: '6'
  }
])
const alarmLevelList = ref([
  {
    name: '低',
    value: '1'
  },
  {
    name: '中',
    value: '2'
  },
  {
    name: '高',
    value: '3'
  }
])
const confirmRow = (row) => {
  http.alarm.update({
    id: row.id,
    status: 1
  }).then(() => {
    search()
  })
}
</script>


<template>
  <PageMain title="实时告警">
    <el-form label-suffix="：" inline>
      <el-form-item label="设备编号">
        <el-input v-model="searchParams.device_no" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="告警类型">
        <el-select style="width: 200px" v-model="searchParams.type" placeholder="请选择">
          <el-option v-for="item in alarmCategoryList" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="告警级别">
        <el-select style="width: 200px" v-model="searchParams.level" placeholder="请选择">
          <el-option v-for="item in alarmLevelList" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="设备编号" prop="device_no">
        </el-table-column>
        <el-table-column label="告警类型" prop="type">
        </el-table-column>
        <el-table-column label="告警等级" prop="level">
          <template #default="scope">
            <el-button size="small" circle :type="scope.row.levelType"></el-button>
            {{scope.row.level}}
          </template>
        </el-table-column>
        <el-table-column label="值" prop="bus_val">
        </el-table-column>
        <el-table-column label="告警信息" prop="message">
        </el-table-column>
        <el-table-column label="告警时间" prop="created_at">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" v-if="scope.row.status === 0" @click="confirmRow(scope.row)">确认告警</el-button>

            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
